package com.oraclejava.multimedia;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.media.MediaPlayer;
import android.view.MotionEvent;
import android.view.View;

public class MediaPlayerView extends View implements
		MediaPlayer.OnCompletionListener {
	private MediaPlayer player;

	// Constructor
	public MediaPlayerView(Context context) {
		super(context);
		setBackgroundColor(Color.WHITE);
		setFocusable(true);
		player = null;
	}


	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		// super.onDraw(canvas);
		Paint paint = new Paint();
		paint.setAntiAlias(true);
		paint.setTextSize(32);
		canvas.drawText("MediaPlayerEx", 0, 40, paint);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if (event.getAction() == MotionEvent.ACTION_DOWN) {
			if (player == null) {
				playSound();
			} else {
				stopSound();
			}
		}
		return true;
	}

	
	public void playSound() {
		try {
			stopSound();

			player = MediaPlayer.create(getContext(), R.raw.sample);
			player.seekTo(0);
			player.start();
			player.setOnCompletionListener(this);
		} catch (Exception e) {
		}
	}
	public void stopSound() {
		try {
			if (player == null)
				return;

			player.stop();
			player.setOnCompletionListener(null);
			player.release();
			player = null;
		} catch (Exception e) {
		}
	}

	
	@Override
	public void onCompletion(MediaPlayer mp) {
		// TODO Auto-generated method stub
		stopSound();
	}

}
